package com.keshan.controller;

import com.keshan.utils.DateUtil;
import com.keshan.utils.Msg;
import com.keshan.pojo.SwitchMachine;
import com.keshan.pojo.SwitchMachineJson;
import com.keshan.service.SwitchMachineServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author keshan
 * @date 2022/10/21 - 19:53
 */
@CrossOrigin
@Controller
@RequestMapping("/switchmachine")
public class SwitchMachineController {
    @Autowired
    SwitchMachineServices switchMachineServices;

    @RequestMapping("/insertswitchmachine")
    @ResponseBody
    public Msg insertBySelectiveToSwitchMachine(@RequestBody SwitchMachineJson switchMachineJson){
        SwitchMachine switchMachine = new SwitchMachine();
        switchMachine.setDeviceType(switchMachineJson.getDevice_type());
        switchMachine.setImei(switchMachineJson.getImei());
        switchMachine.setRemainingPower(switchMachineJson.getRemaining_power());
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMddHHmmss").parse(switchMachineJson.getTime_begin_str());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        switchMachine.setTimeBegin(DateUtil.StringToDate(switchMachineJson.getTime_begin_str()));
        switchMachine.setType(switchMachineJson.getType());
        int result = switchMachineServices.insertBySelectiveSwitchMachine(switchMachine);
        return Msg.success().add("result",result);
    }
}
